<template>
  <div class="app-container wechat-gzh">
    <div v-loading="$store.state.app.loading" class="table-main">
      <div class="title">微信-公众号</div>
      <el-alert type="info" :closable="false">系统微信支付、公众号用户登录会用到该配置，请确保填写填写。</el-alert>
      <el-form ref="itemData" size="small" :model="itemData" label-width="160px" style="width:650px">
        <div class="partition-container">
          <div class="partition-left">
            <el-form-item class="is-required" prop="appid" label="AppId：">
              <el-input v-model="itemData.appid" placeholder="请填写你申请的公众号应用ID" :disabled="isReadonly" />
            </el-form-item>
            <el-form-item class="is-required" prop="secret" label="Secret：">
              <el-input v-model="itemData.secret" placeholder="请填写你公众号的通信密钥" :disabled="isReadonly" />
            </el-form-item>
            <el-form-item prop="token" label="Token：">
              <el-input v-model="itemData.token" placeholder="请填写你公众号的通信令牌" :disabled="isReadonly" />
            </el-form-item>
            <el-form-item prop="encodingAESKey" label="EncodingAESKey：">
              <el-input v-model="itemData.encodingAESKey" placeholder="请填写你公众号的消息加解密密钥" :disabled="isReadonly" />
            </el-form-item>
          </div>
        </div>
        <el-form-item>
          <template v-if="isReadonly">
            <el-button type="primary" :plain="true" @click="handleSubmitForm">修 改</el-button>
          </template>
          <template v-else>
            <el-button :plain="false" @click="fillItemData">取 消</el-button>
            <el-button type="primary" :plain="false" :loading="$store.state.app.loading" @click="handleSubmitForm">保 存</el-button>
          </template>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { wechatGzh, updateWechatGzh } from '@/api/config'

export default {
  data: function() {
    return {
      isReadonly: true,
      params: null,
      itemData: {
        appid: null,
        secret: null,
        token: null,
        encodingAESKey: null
      }
    }
  },
  mounted: function() {
    // 初始化方法
    this.initialize()
  },
  methods: {
    // 初始化数据列表
    initialize: function() {
      // 获取用户列表
      this.$store.commit('app/SET_LOADING', true)
      wechatGzh().then(res => {
        this.params = res.data
        this.fillItemData()
      })
    },
    // 打开数据编辑窗口
    handleSubmitForm: function() {
      if (!this.isReadonly) {
        this.$store.commit('app/SET_LOADING', true)
        updateWechatGzh(this.itemData).then(function(res) {
          this.params = res.data
          this.fillItemData()
        }.bind(this))
      } else {
        this.isReadonly = false
      }
    },
    // 填充数据
    fillItemData: function() {
      this.itemData.appid = this.params.value
      this.itemData.secret = this.params.params.secret
      this.itemData.token = this.params.params.token ? this.params.params.token : null
      this.itemData.encodingAESKey = this.params.params.encodingAESKey ? this.params.params.encodingAESKey : null
      this.isReadonly = true
      this.$nextTick(function() {
        this.$store.commit('app/SET_LOADING', false)
      })
    }
  }
}
</script>

<style lang="scss">
.wechat-xcx {
  .el-form-item label {
    font-weight: normal;
  }
  .partition-right {
    border: none !important;
    .el-form-item__content {
      margin-left: 0 !important;
    }
  }
}
</style>

<style lang="scss" scoped>
.app-container {
  letter-spacing: 0.5px;
  > .title {
    color: #333333;
    font-size: 14px;
    font-weight: 700;
    padding-left: 8px;
    border-left: 2px solid #88B7E0;
  }
  .el-alert {
    border-radius: 0;
    margin: 20px 0 20px 0;
    border: 1px solid #EBEEF5;
  }
}
</style>
